﻿using System;
using System.Collections.Generic;
//using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;
namespace ModbusTestTool
{

 
    
    public partial class FileSelector : Form
    {



        public BindingList<fileSelect> storage;





        public FileSelector()
        {
            this.DragEnter += new DragEventHandler(FileSelector_DragEnter);
            this.DragDrop += new DragEventHandler(FileSelector_DragDrop);
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(FileSelector_FormClosing);


            if (File.Exists("out.xml"))
            {
                using (StreamReader temp = new StreamReader("out.xml"))
                {
                    XmlSerializer outHandle = new XmlSerializer(typeof(BindingList<fileSelect>));
                    storage = (BindingList<fileSelect>)outHandle.Deserialize(temp);
                }
            }
            else
            {
                storage = new BindingList<fileSelect>();
            }


            
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AutoSize = false;
            dataGridView1.DataSource = storage;
            
            DataGridViewColumn filename = new DataGridViewTextBoxColumn();
            filename.Name = "File Name";
            filename.ReadOnly = false;
            filename.DataPropertyName = "filename";
            
            dataGridView1.Columns.Add(filename);


            DataGridViewColumn pollTime = new DataGridViewTextBoxColumn();
            pollTime.Name = "Poll Time";
            pollTime.ReadOnly = false;
            pollTime.DataPropertyName = "pollRate";
            dataGridView1.Columns.Add(pollTime);

            DataGridViewColumn outputfilenameColumn = new DataGridViewTextBoxColumn();
            outputfilenameColumn.Name = "Output File Name";
            outputfilenameColumn.ReadOnly = false;
            outputfilenameColumn.DataPropertyName = "outputfile";
            dataGridView1.Columns.Add(outputfilenameColumn);

            //this.AutoSize = true;

         

        
        
        }

        void FileSelector_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string item in files)
                {
                    storage.Add(new fileSelect(item, 100));
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Make sure you are not editing something");
                MessageBox.Show(err.Message);
            }
        }

        void FileSelector_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                e.Effect = DragDropEffects.All;
        }

        void FileSelector_FormClosing(object sender, FormClosingEventArgs e)
        {
            //XmlSerializer outHandle = new XmlSerializer(typeof(BindingList<fileSelect>));
            using (StreamWriter temp = new StreamWriter("out.xml"))
            {
                XmlSerializer outHandle = new XmlSerializer(typeof(BindingList<fileSelect>));
                outHandle.Serialize(temp, storage);
            }
            
        }
    }

    public class fileSelect
    {
        public fileSelect()
        {
            filename = "";
            _pollRate = 100;
        }

        public fileSelect(string infilename, int inpollRate)
        {
            filename = infilename;
            _pollRate = inpollRate;
        }
        public string outputfile { set; get; }
        public string filename { set; get; }
        private int _pollRate;
        public string pollRate
        {
            set
            {
                _pollRate = int.Parse(value);
            }
            get
            {
                return _pollRate.ToString();
            }
        }
    }
}
